/*
 * Created on Feb 14, 2005
 * Created by dfhuynh
 */
package edu.mit.simile.longwell.ui;

import java.net.URLEncoder;

import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.Resource;

import edu.mit.simile.longwell.LongwellServer;
import edu.mit.simile.longwell.Profile;
import edu.mit.simile.longwell.query.Query;
import edu.mit.simile.longwell.query.bucket.DistinctValueBucketer;
import edu.mit.simile.longwell.query.project.PropertyProjector;
import edu.mit.simile.longwell.schema.ISchemaModel;
import edu.mit.simile.longwell.schema.IStructuredProperty;

/**
 * @author dfhuynh
 */
public class FlairURL {
	final public String	m_contextPath;
	final public String	m_profileID;
	final public Query	m_query;
	public FlairURL(
		String	contextPath,
		String	profileID,
		Query	query
	) {
		m_contextPath = contextPath;
		m_profileID = profileID;
		m_query = new Query(query);
	}

	
	public FlairURL(FlairURL furl) {
		this(furl.m_contextPath, furl.m_profileID, furl.m_query);
	}
	
	public FlairURL(FlairMessage msg) {
		this(
			msg.m_request.getContextPath(),
			msg.m_profileID,
			msg.m_query);
	}


	public FlairURL dup() {
		return new FlairURL(this);
	}
	
	public FlairURL changeCommand(String command) {
		FlairURL url = new FlairURL(
			m_contextPath,
			m_profileID,
			m_query
		);
		
		url.getQuery().setParameter("command", command);
		
		return url;
	}
	
	public FlairURL changeCommandProfile(String command, String profileID) {
		FlairURL url = new FlairURL(
			m_contextPath,
			profileID,
			m_query
		);
		
		url.getQuery().setParameter("command", command);
		
		return url;
	}
	
	public FlairURL changeCommandQuery(String command, String query) {
		FlairURL url = new FlairURL(
			m_contextPath,
			m_profileID,
			new Query(query)
		);
		url.getQuery().setParameter("command", command);	
		return url;
	}

	public FlairURL changeCommandQuery(String command,String query, String[] values) {
		FlairURL url = new FlairURL(
			m_contextPath,
			m_profileID,
			new Query(query)
		);
		
		url.getQuery().setParameter("command", command);	
		return url;
	}	

	public FlairURL changeCommandQuery(String command, String query, String graphName) {
		FlairURL url = new FlairURL(
			m_contextPath,
			m_profileID,
			new Query(query)
		);
		//url.getQuery().addRestriction("edu.mit.simile.longwell.query.project.SchemaBasedProjector", "",  "edu.mit.simile.longwell.query.bucket.DistinctValueBucketer", "http://www.semanticweb.org/ontologies/2011/9/Rivela-Flussi.owl#");
		//url.getQuery().addRestriction("edu.mit.simile.longwell.query.project.SchemaBasedProjector", "",  "edu.mit.simile.longwell.query.bucket.DistinctValueBucketer", "rhttp://www.ontologydesignpatterns.org/ont/dul/DUL.owl#");
		
		url.getQuery().setParameter("command", command);
		return url;
	}	
	
	public FlairURL changeQuery(Query query) {
		FlairURL url = new FlairURL(
			m_contextPath,
			m_profileID,
			query
		);
		
		String command = getCommand();
		if (command != null) {
			url.getQuery().setParameter("command", command);
		}
		
		return url;
	}

	public FlairURL changeQuery(String query) {
		FlairURL url = new FlairURL(
			m_contextPath,
			m_profileID,
			new Query(query)
		);
		
		String command = getCommand();
		if (command != null) {
			url.getQuery().setParameter("command", command);
		}
		
		return url;
	}
	
	public String getContextPath() {
		return m_contextPath;
	}
	
	public String getCommand() {
		return m_query.getFirstParamValue("command");
	}
	
	public String getProfileID() {
		return m_profileID != null ? m_profileID : 
			//LongwellServer.getServer().getDefaultProfile().getID();
			LongwellServer.getServer().getProfile(m_profileID).getID();
	}
	
	public Profile getProfile() {
		return LongwellServer.getServer().getProfile(getProfileID());
	}
	
	public Query getQuery() {
		return m_query;
	}
	
	public String toURLString() {
		String s = m_contextPath;
		
		if (m_profileID != null) {
			s += "/" + URLEncoder.encode(m_profileID);
		}
		
		s += "?" + m_query.toURLQueryString();
		
		return s;
	}

	public String getNarrowingURL(Resource property, RDFNode value, boolean clearOtherRestrictions, boolean forward) {
		Profile profile = getProfile();
		ISchemaModel schemaModel = profile.getSchemaModel();
		
		if (forward) {
			IStructuredProperty sProperty =
				schemaModel.getStructuredProperty(
					profile.getModel().createProperty(property.getURI()));
			
			if (sProperty != null) {
				if (sProperty.getTypeConfidence(IStructuredProperty.s_type_integer) > 0.5) {
					return "";
				} else if (sProperty.getTypeConfidence(IStructuredProperty.s_type_dateTime) > 0.5) {
					return "";
				}
		
				String projectorName = PropertyProjector.class.getName();
				String bucketerName = DistinctValueBucketer.class.getName();
				String bucketerParameter = value instanceof Literal ?
						("l" + ((Literal) value).getString()) :
						("r" + schemaModel.resourceToPseudoURI((Resource)value));
						
				FlairURL flairURL = clearOtherRestrictions ?
					new FlairURL(this).changeCommandQuery("browse", "") :
					new FlairURL(this).changeCommand("browse");
				
				flairURL.getQuery().addRestriction(
					projectorName, property.getURI(), 
					bucketerName, bucketerParameter);
				
				return flairURL.toURLString();
			} else {
				return "";
			}
		} else {
			String projectorName = PropertyProjector.class.getName();
			String bucketerName = DistinctValueBucketer.class.getName();
			String bucketerParameter = 
				"r" + schemaModel.resourceToPseudoURI((Resource)value);
					
			FlairURL flairURL = clearOtherRestrictions ?
				new FlairURL(this).changeCommandQuery("browse", "") :
				new FlairURL(this).changeCommand("browse");
			
			flairURL.getQuery().addRestriction(
				projectorName, "!" + property.getURI(), 
				bucketerName, bucketerParameter);
			
			return flairURL.toURLString();
		}
	}
}
